<?php

namespace Home\Controller;

use Think\Controller;

class CommonController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if (!session('index_user.id')) {
            $this->error('未登录', U('Authen/login'));
        }

    }
    /*
     * 得到导航菜单我的页面组的用户头部信息
     */
    public function getUserInfo()
    {
        $user_info = session('index_user');
        $user_info['age'] = ceil((time() - $user_info['create_time']) / (24 * 60 * 60));//计算注册天数
        $map['teacherid'] = session('index_user.id');
        $user_info['billnum'] = M('plan')->where($map)->count();//总预约数字，需要动态计算
        if(!$user_info['billnum'])
            $user_info['billnum'] =0;
        return $user_info;
    }

    /**
     * @return string上传图片,成功后返回图片路径
     */
    public function uploadpho($name){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类
        $upload->rootPath=      APP_PATH.'/Public/'; // 设置附件上传目录    // 上传单个文件
        $upload->savePath  =      './Uploads/'; // 设置附件上传目录    // 上传单个文件
        // 上传文件
        $info   =   $upload->uploadOne($_FILES[$name]);//源码采用相对路径，必须用绝对路径
        $info['savepath']=explode('.',$info['savepath']);
        $info['savepath']=$info['savepath'][1];
        $filename = APP_PATH.'/Public'.$info['savepath'].$info['savename'];
        session('file',$info);
        $exts = $info['ext'];
        if($filename){
            //制作缩略图
            $image = new \Think\Image();
            $image->open($filename); // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            //生成缩略图的地址
            $path1='a'.time().'.'.$exts;
            $path=APP_PATH.'Public'.$info['savepath'].$path1;
            $type=$image->thumb(150, 150,2)->save($path);
            if($type){
                unlink($filename);
                return date("Y-m-d", time()).'/'.$path1;
            }else{
                $this->error('上传图片失败');exit();
            }
        }else{
            $this->error('上传图片失败');exit();
        }
    }
    /**
     * 用户预约
     */
    public function yuyue()
    {
        $data = I('post.');//服务id ,用户手机，用户备注员工id
        $data['userid'] = session('index_user.id');
        $data['create_time'] = time();
        $tag = M('yuyue')->add($data);
        if($tag)
            $this->success('预约成功');
        else
            $this->error('预约失败');
    }


    /**
     * 详情信息
     */
    public function details(){
        $data=I('post.');
        $info['msg'] = 'error';
        $serveinfo = [];
        if($data['type'] == 'tempwork'){
            $serveinfo = M('tempwork')
                ->find($data['id']);
        }else if($data['type'] == 'yuesao'){
            $serveinfo = M('yuesao')->find($data['id']);
        }else if($data['type'] == 'peihu'){
            $serveinfo = M('peihu')->find($data['id']);
        }
        if($serveinfo){
            $info['data'] = $serveinfo;
            $staffinfo = M('user')->find($serveinfo['staffid']);
            $info['data']['phone'] = $staffinfo['phone'];
            $info['msg'] = 'ok';
        }else{
            $info['data'] = [];
        }
        $this->ajaxReturn($info);
    }

    /**
     * 删除服务
     */
    public function delete(){
        $data=I('get.');
        $tag=false;
        if($data['type'] == 'tempwork'){
            $tag = M('tempwork')->delete($data['id']);
        }else if($data['type'] == 'yuesao'){
            $tag = M('yuesao')->delete($data['id']);
        }else if($data['type'] == 'peihu'){
            $tag = M('peihu')->delete($data['id']);
        }
        if($tag)
            $this->success('删除成功');
        else
            $this->error('删除失败');

    }

}